<?php
class CharityCategoriesController extends AppController
{
    var $name = 'CharityCategories';
    function admin_index()
    {
        $this->_redirectGET2Named(array(
            'q',
        ));
        $this->pageTitle = __l('Charity Categories');
        $this->CharityCategory->recursive = 0;
        if (isset($this->params['named']['q']) && !empty($this->params['named']['q'])) {
            $this->paginate['search'] = $this->params['named']['q'];
            $this->data['CharityCategory']['q'] = $this->params['named']['q'];
        }
        $moreActions = $this->CharityCategory->moreActions;
        $this->set(compact('moreActions'));
        $this->set('charityCategories', $this->paginate());
    }
    function admin_add()
    {
        $this->pageTitle = __l('Add Charity Category');
        if (!empty($this->data)) {
            $this->CharityCategory->create();
            if ($this->CharityCategory->save($this->data)) {
                $this->Session->setFlash(__l('Charity Category has been added') , 'default', null, 'success');
                $this->redirect(array(
                    'action' => 'index'
                ));
            } else {
                $this->Session->setFlash(__l('Charity Category could not be added. Please, try again.') , 'default', null, 'error');
            }
        }
    }
    function admin_edit($id = null)
    {
        $this->pageTitle = __l('Edit Charity Category');
        if (is_null($id)) {
            $this->cakeError('error404');
        }
        if (!empty($this->data)) {
            if ($this->CharityCategory->save($this->data)) {
                $this->Session->setFlash(__l('Charity Category has been updated') , 'default', null, 'success');
                $this->redirect(array(
                    'action' => 'index'
                ));
            } else {
                $this->Session->setFlash(__l('Charity Category could not be updated. Please, try again.') , 'default', null, 'error');
            }
        } else {
            $this->data = $this->CharityCategory->read(null, $id);
            if (empty($this->data)) {
                $this->cakeError('error404');
            }
        }
        $this->pageTitle.= ' - ' . $this->data['CharityCategory']['name'];
    }
    function admin_delete($id = null)
    {
        if (is_null($id)) {
            $this->cakeError('error404');
        }
        if ($this->CharityCategory->del($id)) {
            $this->Session->setFlash(__l('Charity Category deleted') , 'default', null, 'success');
            $this->redirect(array(
                'action' => 'index'
            ));
        } else {
            $this->cakeError('error404');
        }
    }
    function admin_update()
    {
        if (!empty($this->data[$this->modelClass])) {
            $r = $this->data[$this->modelClass]['r'];
            $actionid = $this->data[$this->modelClass]['more_action_id'];
            unset($this->data[$this->modelClass]['r']);
            unset($this->data[$this->modelClass]['more_action_id']);
            $ids = array();
            foreach($this->data[$this->modelClass] as $id => $is_checked) {
                if ($is_checked['id']) {
                    $ids[] = $id;
                }
            }
            if ($actionid && !empty($ids)) {
                switch ($actionid) {
                    case ConstMoreAction::Delete:
                        foreach($ids as $id) {
                            $this->{$this->modelClass}->deleteAll(array(
                                $this->modelClass . '.id' => $id
                            ));
                        }
                        $this->Session->setFlash(__l('Checked charity categories has been deleted') , 'default', null, 'success');
                        break;
                }
            }
        }
        $this->redirect(Router::url('/', true) . $r);
    }
}
?>